// File: database/DatabaseConnection.cpp
#include "DatabaseConnection.h"
#include <QDebug>

QSqlDatabase DatabaseConnection::getConnection() {
    QSqlDatabase db = QSqlDatabase::database();
    if (!db.isValid()) {
        // 获取应用数据存储路径
        QString dataPath = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation)+"/db";
        QDir dataDir(dataPath);
        qDebug() << "数据库存储目录是: " << dataPath;

        // 确保目录存在
        if (!dataDir.exists()) {
            dataDir.mkpath(dataPath);
        }

        // 设置完整数据库路径
        QString dbPath = dataPath + "/novel_info.db";

        db = QSqlDatabase::addDatabase("QSQLITE");
        db.setDatabaseName(dbPath);
    }
    return db;
}

void DatabaseConnection::initialize() {
    QSqlDatabase db = getConnection();
    if (db.open()) {
        QSqlQuery query;

        //小说表
        QString novelTable = "CREATE TABLE IF NOT EXISTS novel_info ("
                        "id TEXT PRIMARY KEY,"
                        "title TEXT NOT NULL,"
                        "author TEXT DEFAULT '用户',"
                        "cover_path TEXT,"
                        "description TEXT,"
                        "category TEXT,"
                        "tags TEXT,"
                        "language TEXT DEFAULT 'zh-CN',"
                        "status INTEGER DEFAULT 0,"
                        "chapters_count INTEGER DEFAULT 0,"
                        "word_count INTEGER DEFAULT 0,"
                        "publish_time TEXT DEFAULT CURRENT_TIMESTAMP,"
                        "create_time TEXT DEFAULT CURRENT_TIMESTAMP,"
                        "update_time TEXT DEFAULT CURRENT_TIMESTAMP,"
                        "last_read_chapter TEXT,"
                        "file_size INTEGER DEFAULT 0,"
                        "source_type INTEGER DEFAULT 0,"
                        "source_url TEXT,"
                        "parse_progress REAL DEFAULT 0.0)";

        if (query.exec(novelTable)) {
            qDebug() << "创建小说表成功！";
        } else {
            qDebug() << "创建小说表失败，信息:" << query.lastError().text();
        }


        //章节表
        QString chapterTable = "CREATE TABLE IF NOT EXISTS chapter_info ("
                    "id TEXT PRIMARY KEY,"
                    "title TEXT,"
                    "novel_id TEXT,"
                    "chapter_index INTEGER,"
                    "path TEXT,"
                    "word_count INTEGER,"
                    "start_offset INTEGER,"
                    "end_offset INTEGER,"
                    "prev_chapter TEXT,"
                    "next_chapter TEXT,"
                    "load_status INTEGER,"
                    "status INTEGER)";
        
        if (query.exec(chapterTable)) {
            qDebug() << "创建小说章节表成功！";
        } else {
            qDebug() << "创建小说章节表失败，信息:" << query.lastError().text();
        }
        
        //阅读进度表
        query.exec("CREATE TABLE IF NOT EXISTS novel_read_process ("
                    "id TEXT PRIMARY KEY,"
                    "user_id TEXT,"
                    "novel_id TEXT,"
                    "last_read_chapter_id TEXT,"
                    "progress REAL,"
                    "last_position INTEGER)");

        //时间轴表
        query.exec("CREATE TABLE IF NOT EXISTS time_line_log ("
                    "id INTEGER PRIMARY KEY AUTOINCREMENT,"
                    "user_id INTEGER,"
                    "action_type TEXT NOT NULL,"
                    "action_time TEXT DEFAULT (datetime('now', 'localtime')),"
                    "action_module TEXT,"
                    "action_detail TEXT,"
                    "ip_address TEXT");

    }
}
